package chapter09.p;

import java.io.IOException;
import java.nio.CharBuffer;
import java.util.Random;

public class P16 implements Readable {
	private static Random rand = new Random(47);
	private static final char[] capitals =
		"ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
	private static final char[] lowers =
		"abcdefghijklmnopqrstuvwxyz".toCharArray();
	private static final char[] digits =
		"0123456789".toCharArray();
	private int count;

	public P16(int count) {
		this.count = count;
	}
	public char[] generate() {
		if (count -- == 0) {
			return null;
		}
		StringBuffer buffer = new StringBuffer();
		buffer.append(capitals[rand.nextInt(capitals.length)]);
		for (int i = 0; i < 4; i++) {
			buffer.append(lowers[rand.nextInt(lowers.length)]);
			buffer.append(digits[rand.nextInt(digits.length)]);
		}
		return buffer.toString().toCharArray();
	}
	public int read(CharBuffer cb) throws IOException {
		cb.append(generate().toString());
		return cb.length();
	}
}
